import Vue from 'vue';
import Router from 'vue-router';
import { fullRoutes, RouteType } from './routes';

Vue.use(Router);

// 得到完整路由表
const getMappedRoutes = (r: Array<RouteType>): Array<RouteType> => (
  r.map(route => ({
    ...route,
    // 按需加载
    component: () => import(`@/views/${route.page}.vue`),
    children: route.children ? getMappedRoutes(route.children) : undefined,
  }))
);

const baseRoutes: Array<RouteType> = [{
  path: '/Index',
  name: '首页',
  page: 'login/Initial',
}, {
  path: '/Login',
  name: '登录',
  page: 'login/Login',
}, {
  path: '/Register',
  name: '注册',
  page: 'login/Register',
}, {
  path: '/404',
  name: '404页面',
  page: 'common/NotFound',
},];

let commonRoute = [{
  path: '/',
  name: '默认',
  page: 'login/Initial',
}, {
  path: '*',
  name: '全局',
  page: 'login/Login',
}];


const nowRoutes = baseRoutes.concat(fullRoutes).concat(commonRoute);
const routes = getMappedRoutes(nowRoutes);
console.warn(routes);

const router = new Router({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes,
});

export {
  router,
  routes,
}
